package com.vf.admin.miaodong.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.vf.admin.client.common.RedisKeys;
import com.vf.admin.client.service.AppRealNameService;
import com.vf.admin.common.redis.RedisUtil;
import com.vf.admin.common.splitpage.SplitPageBean;
import com.vf.admin.fiwork.util.DateUtils;
import com.vf.admin.miaodong.dao.AfterUserDao;
import com.vf.admin.miaodong.entity.AppRealName;
import com.vf.admin.miaodong.entity.AppUser;
import com.vf.admin.miaodong.service.AfterUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 用户管理
 */
@Service
public class AfterUserServiceImpl implements AfterUserService {

    @Autowired
    AfterUserDao afterUserDao;
    @Autowired
    AppRealNameService realNameService;
    @Autowired
    RedisUtil redisUtil;

    /**
     * 详情
     */
    @Override
    public AppUser get(Integer id) {
        return afterUserDao.get(id);
    }

    /**
     * 添加/修改
     */
    @Override
    public void save(AppUser haoYou) {
        afterUserDao.save(haoYou);
    }

    /**
     * 综合查询列表
     */
    @Override
    public List<AppUser> findList(Map<String, Object> map, SplitPageBean bean) {
        List<AppUser> list = afterUserDao.findList(map, bean);
        for(AppUser u : list) {
            AppRealName real = realNameService.getReal(u.getId());
            if(real != null) {
                u.setIdnumjust(real.getIdnumjust());
                u.setShipinstate1(real.getStatus());
            }

        }
        return list;
    }

    @Override
    public List<AppUser> findList1(Map<String, Object> map, SplitPageBean bean) {
        List<AppUser> list = afterUserDao.findList1(map, bean);
        for(AppUser u : list) {
           u.setLoginname(u.getLoginname().substring(0, 3) + "****" + u.getLoginname().substring(7));
        }
        return list;
    }
    /**
     * 综合查询列表
     */
    @Override
    public AppUser xiajiZongCzMoney(String code) {
        return afterUserDao.xiajiZongCzMoney(code);
    }

    /**
     * 根据邀请码查询用户
     *
     * @param code
     * @return
     */
    @Override
    public AppUser getUserByCode(String code) {
        return afterUserDao.getUserByCode(code);
    }

    /**
     * 账号状态修改
     * @param id
     * @param state
     */
    @Override
    public void upstate(Integer id, Integer state, Integer hour) {
        if(state == 2 && hour != null) {
            //-1为永久禁止 不做redis超时监听
            if(hour != -1) {
                String key = String.format(RedisKeys.FORBID_USER_LOGIN, id);
                redisUtil.set(key, id, DateUtils.hourToSeconds(hour));
            }
        } else if(state == 1) {
            String key = String.format(RedisKeys.FORBID_USER_LOGIN, id);
            redisUtil.del(key);
        }
        afterUserDao.upstate(id,state);
    }

    /**
     * 修改用户视频直播权限
     * @param id
     * @param state
     */
    @Override
    public void upstateVideoStatus(Integer id, Integer state) {
        afterUserDao.upstateVideoStatus(id,state);
    }

    /**
     * 修改用户实名认证状态
     * @param id
     * @param state
     */
    @Override
    public void upstateShiming(Integer id, Integer state) {
        afterUserDao.upstateShiming(id,state);
    }    @Override

    /**
     * 修改用户实名认证状态
     * @param id
     * @param state
     */
    public void upstateShipin(Integer id, Integer state) {
        afterUserDao.upstateShipin(id,state);
    }

    /**
     * 修改用户语音直播权限
     * @param id
     * @param state
     */
    @Override
    public void upstateYuyinStatus(Integer id, Integer state) {
        afterUserDao.upstateYuyinStatus(id,state);
    }

    /*
     * @Description 更新用户金币
     * @Param: [id, jinbi]
     * @return: void
     * @Author: ljx
     * @Date: 2021/07/22
     */
    @Override
    public void updatejinbi(Integer id, BigDecimal jindou) {
        afterUserDao.updatejinbi(id, jindou);
        String key = String.format(RedisKeys.USER, id);
        redisUtil.del(key);
    }


    /*
     * @Description 更新钻石数量
     * @Param: [id, zuanshi]
     * @return: void
     * @Author: ljx
     * @Date: 2021/07/24
     */
    @Override
    public void updateZuanShi(Integer id, BigDecimal zuanshi) {
        afterUserDao.updateZuanShi(id, zuanshi);
        String key = String.format(RedisKeys.USER, id);
        redisUtil.del(key);
    }

    /**
     * 减少用户金币
     *
     * @Param: [id, jinbi]
     * @return: void
     * @Author: zhouyunyi
     * @Date: 2021/07/28
     * @Time: 16:08
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void upJianjinbi(Integer id, BigDecimal jindou) {
        afterUserDao.upJianjinbi(id, jindou);
        String key = String.format(RedisKeys.USER, id);
        redisUtil.del(key);
    }

    /**
     * 减少钻石数量
     *
     * @Param: [id, zuanshi]
     * @return: void
     * @Author: zhouyunyi
     * @Date: 2021/07/28
     * @Time: 16:09
     */
    @Override
    public void upJianZuanShi(Integer id, Integer zuanshi) {
        afterUserDao.upJianZuanShi(id, zuanshi);
        String key = String.format(RedisKeys.USER, id);
        redisUtil.del(key);
    }

    /**
     * 减少魅力值
     *
     * @Param: [id, meili]
     * @return: void
     * @Author: zhouyunyi
     * @Date: 2021/07/28
     * @Time: 16:09
     */
    @Override
    public void upJianmeili(Integer id, Integer meili) {
        afterUserDao.upJianmeili(id, meili);
        String key = String.format(RedisKeys.USER, id);
        redisUtil.del(key);
    }

    @Override
    public AppUser getByUserCodeOrGoodCodeExistUserId(Integer userId, Integer userCode) {
        return afterUserDao.getByUserCodeOrGoodCodeExistUserId(userId, userCode);
    }

    @Override
    public void updateGoodUserCode(Integer userId, Integer goodUserCode) {
        afterUserDao.updateGoodUserCode(userId, goodUserCode);
        String key = String.format(RedisKeys.USER, userId);
        redisUtil.del(key);
    }

    @Override
    public void upWhiteStatus(Integer id,Integer state) {
        afterUserDao.upWhiteStatus(id,state);
    }

    /*
     * @Description 更新魅力值数量
     * @Param: [id, meili]
     * @return: void
     * @Author: ljx
     * @Date: 2021/07/24
     */
    @Override
    public void updatemeili(Integer id, Integer meili) {
        afterUserDao.updatemeili(id, meili);
        String key = String.format(RedisKeys.USER, id);
        redisUtil.del(key);
    }
}
